#!/bin/bash

#--------------------------------------------
# 功能：创建符合itms-services协议的发布文件
# 作者：ccf
# E-mail:ccf.developer@gmail.com
# 创建日期：2012/09/24
#--------------------------------------------
# 修改日期：2012/09/27
# 修改人：ccf
# 修改内容：去掉打包的部分脚本，只保留生成协议文件部分，以后此脚本依赖ipa-build脚本生成的内容
#--------------------------------------------
# 修改日期：2013/02/18
# 修改人：ccf
# 修改内容：添加通过sftp上传到服务器的功能
#--------------------------------------------
# 修改日期：2013/02/19
# 修改人：ccf
# 修改内容：添加上传服务器后邮件提醒功能
#--------------------------------------------
# 修改日期：2013/02/27
# 修改人：ccf
# 修改内容：添加更新服务器上工程索引文件功能
#--------------------------------------------

#须配置内容  start

#sftp参数设置
sftp_server=192.168.xx.xx
sftp_username=xx
sftp_password=xx
sftp_workpath="/usr/share/xx/xx/xx"



#发布应用的url地址
pulish_url="http://xx.com/xx"

#以下是邮箱的相关设置
#收件人
email_reciver=xx@xx.com
#发送者邮箱
email_sender=xx@xx.com
#邮箱用户名
email_username=xx
#邮箱密码
email_password=xx
#smtp服务器地址
email_smtphost=smtp.exmail.qq.com


#可配置内容  end






#获取shell文件所在的绝对路径
current_path=$(pwd)
tmp_path=$(dirname $0)
cd $tmp_path
shell_path=$(pwd)
cd $current_path



#参数判断
if [ $# != 2 ] && [ $# != 1 ];then
	echo "Number of params error! Need one or two params!"
	echo "1.root path of project(necessary) 2.should send email after upload?y/n(default is n)"
	exit
elif [ ! -d $1 ];then
	echo "The param must be a dictionary."
	exit	
	
fi

should_email=n

if [ $# = 2 ];then
	should_email=$2
	if [ $should_email != y ] && [ $should_email != n ];then
		echo "Error! The Second Param must be y or n!"
		exit
	fi
fi



#工程绝对路径
cd $1
project_path=$(pwd)

#判断所输入路径是否是xcode工程的根路径
ls | grep .xcodeproj > /dev/null
rtnValue=$?
if [ $rtnValue != 0 ];then
	echo "Error!! The param must be the root path of a xcode project."
	exit
fi

#判断是否执行过ipa-build脚本
ls ./build/ipa-build/*.ipa &>/dev/null
rtnValue=$?
if [ $rtnValue != 0 ];then
	echo "Error!! No ipa files exists.Please run the \"ipa-build\" shell script first"
	exit
fi


#build文件夹路径
build_path=${project_path}/build

#切换到tmp文件夹
cd /tmp
#创建临时文件夹
tmpfoldername=ipa_tmp
if [ -d ./${tmpfoldername} ];then
	rm -rf ${tmpfoldername}
fi
mkdir ${tmpfoldername}

cd ${tmpfoldername}
#拷贝ipa到临时文件夹中
cp ${build_path}/ipa-build/*.ipa ./tmp.zip
#将ipa解压
unzip tmp.zip
#app文件中Info.plist文件路径
app_infoplist_path=$(pwd)/Payload/*.app/Info.plist
#取版本号
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" ${app_infoplist_path})
#取build值
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" ${app_infoplist_path})
#取bundleIdentifier
bundleIdentifier=$(/usr/libexec/PlistBuddy -c "print CFBundleIdentifier" ${app_infoplist_path})
#取CFBundleName
target_name=$(/usr/libexec/PlistBuddy -c "print CFBundleName" ${app_infoplist_path})
#取CFBundleDisplayName
display_name=$(/usr/libexec/PlistBuddy -c "print CFBundleDisplayName" ${app_infoplist_path})

#删除临时文件夹
cd ..
rm -rf ${tmpfoldername}

#进入到工程build路径下
cd $build_path

#显示名称
ipa_name="${display_name}"


if [ -d ./$target_name ];then
	rm -rf $target_name
fi
mkdir $target_name
#拷贝ipa
cp ./ipa-build/*.ipa ./$target_name/${target_name}.ipa
cp ../Icon@2x.png ./$target_name/${target_name}_logo.png
cd $target_name

#ipa下载url
ipa_download_url=${pulish_url}/${target_name}/${target_name}.ipa
#itms-services协议串
ios_install_url="itms-services://?action=download-manifest&url=${pulish_url}/${target_name}/${target_name}.plist"

#生成install.html文件
cat << EOF > index.html
<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>安装此软件</title>
  </head>
  <body>
	<br>
	<br>
	<br>
	<br>
	<p align=center>
		<font size="8">
			<a href="${ios_install_url}">点击这里安装</a>
		</font>
	</p>
	
    </div>
  </body>
</html>
EOF
#生成plist文件
cat << EOF > ${target_name}.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
   <key>items</key>
   <array>
       <dict>
           <key>assets</key>
           <array>
               <dict>
                   <key>kind</key>
                   <string>software-package</string>
                   <key>url</key>
                   <string>${ipa_download_url}</string>
               </dict>
               <dict>
                   <key>kind</key>
                   <string>display-image</string>
                   <key>needs-shine</key>
                   <true/>
                   <key>url</key>
                   <string>${pulish_url}/${target_name}/${target_name}_logo.png</string>
               </dict>
	       <dict>
                   <key>kind</key>
                   <string>full-size-image</string>
                   <key>needs-shine</key>
                   <true/>
                   <key>url</key>
                   <string>${pulish_url}/${target_name}/${target_name}_logo.png</string>
               </dict>
           </array><key>metadata</key>
           <dict>
               <key>bundle-identifier</key>
               <string>${bundleIdentifier}</string>
               <key>bundle-version</key>
               <string>${bundleVersion}</string>
               <key>kind</key>
               <string>software</string>
               <key>subtitle</key>
               <string>${ipa_name}</string>
               <key>title</key>
               <string>${ipa_name}</string>
           </dict>
       </dict>
   </array>
</dict>
</plist>

EOF

#调用upload脚本来将文件上传到服务器
${shell_path}/uploadItemsServicesFiles $target_name ${sftp_server} ${sftp_username} ${sftp_password} ${sftp_workpath} || exit

#切换到tmp目录下
serverIndexHtmlName=index.html
cd /tmp
rm ${serverIndexHtmlName}
#从服务器下载index.html
${shell_path}/sftpDownloadFile ${serverIndexHtmlName} ${sftp_server} ${sftp_username} ${sftp_password} ${sftp_workpath} || exit
#更新index.html
${shell_path}/updateLocalIndexHtml ${display_name} ${bundleShortVersion} ${ipa_download_url} ${ios_install_url} || exit
#上传index.html
${shell_path}/sftpUploadFile ${serverIndexHtmlName} ${sftp_server} ${sftp_username} ${sftp_password} ${sftp_workpath} || exit



#发送邮件
if [ $should_email = y ];then
	email_title="${ipa_name}-IOS客户端更新"
	email_content="hi,\n\n    The app is updated recently.Use the safari browser on IOS device to download the app.Here is the URL: ${pulish_url}/${target_name} \n\n   To see the list of all iOS projects, open this URL: ${pulish_url} \n\n\n   This email is sent by the automantic shell which is created by ccf,so do not reply this email.\n\n\n\n\n  Thanks!"
	${shell_path}/sendEmail -f ${email_sender} -t ${email_reciver} -s ${email_smtphost} -u ${email_title} -xu ${email_username} -xp ${email_password} -m ${email_content}
fi

